package com.zf.shopping.controller;


import com.zf.shopping.bean.GetResult;
import com.zf.shopping.bean.Result;
import com.zf.shopping.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;


/**
 * 管理员
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 登录
     * @param password
     * @param number
     * @param session
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public Result login(@RequestParam("password") String password, @RequestParam("number") Long number, HttpSession session) {

        Result result = adminService.login(password, number);
        if (result.getCode() == 200) {
            session.setAttribute("admin", number);
        }
        return result;
    }


    /**
     * 登出
     * @param session
     * @return
     */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    @ResponseBody
    public Result logout(HttpSession session) {

        session.removeAttribute("admin");
        return GetResult.success();
    }
}
